const images = require("images");
function shrink(file, width = 200) {
  const index = file.lastIndexOf('.');
  const index1 = file.lastIndexOf('/');
  let filename = file.substring(index1 + 1, index);
  filename += '-small';
  const format = file.substring(index, file.length);
  const path = file.substring(0, index1);
  const newFile = path + '/' + filename + format;
  try {
    images(file)
      .size(width)
      .save(newFile, {
        quality : 100
      });
    return newFile;
  } catch (e) {
    console.log(e.message, '******************图片处理出错****************');
  }
}

function fileNameFormat(originalName, padding) {
  const index = originalName.lastIndexOf('.');
  let filename = originalName.substring(0, index);
  filename += padding;
  const format = originalName.substring(index, originalName.length);
  return filename + format;
}
module.exports = {
  shrink,
  fileNameFormat
}